package com.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.constant.MessageConstant;
import com.exception.CustomException;
import com.mapper.OrdersettingMapper;
import com.pojo.Ordersetting;
import com.pojo.OrdersettingExample;
import com.service.OrdersettingService;
import com.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = OrdersettingService.class)
public class OrdersettingServiceImpl implements OrdersettingService{

    @Autowired
    private OrdersettingMapper ordersettingMapper;

    @Transactional
    @Override
    public void addBatch(List<Ordersetting> ordersetting) throws CustomException {
        //创建一个新的集合来装要添加的数据
        List<Ordersetting> add=new ArrayList<>();
        //1.遍历循环List <OrderSetting>
        for(Ordersetting os:ordersetting){
            //2.通过日期判断是否存在预约设置信息
            OrdersettingExample ose=new OrdersettingExample();
            OrdersettingExample.Criteria osec = ose.createCriteria();
            osec.andOrderdateEqualTo(os.getOrderdate());
            List<Ordersetting> list = ordersettingMapper.selectByExample(ose);
            if(list!=null && list.size()>0){
                //2.1存在 判断已预约人数是否大于 要更新的 可预约人数
                /**
                 * 大于 ：则抛出异常
                 * 小于 ：则更新可预约数
                 * */
                Ordersetting setting = list.get(0);
                if(setting.getReservations()!=null && setting.getReservations()>os.getNumber()){
                    //大于 ：则抛出异常
                    throw new CustomException(MessageConstant.ORDERSETTING_FAIL);
                }else{
                    //小于 ：则更新可预约数
                    os.setId(setting.getId());
                    ordersettingMapper.updateByPrimaryKeySelective(os);
                }
            }else{
                add.add(os);
            }
        }
        //判断新的集合里面有没有数据
        if(add.size()>0){
            //添加
            ordersettingMapper.addBatch(add);
        }
    }

    @Transactional
    @Override
    public void setOrderSettingByDate(Ordersetting os) throws CustomException {
        //1.判空
        if(os.getNumber() != null && os.getOrderdate()!=null){
            //2.根据选择要预约设置的日期进行查询 得到一个对象
            OrdersettingExample ose=new OrdersettingExample();
            OrdersettingExample.Criteria osec = ose.createCriteria();
            osec.andOrderdateEqualTo(os.getOrderdate());
            List<Ordersetting> list = ordersettingMapper.selectByExample(ose);
            //3.通过日期查询判断该日期是否存在
            if(list!=null && list.size()>0){
                for (Ordersetting oss:list){
                        //3.1存在
                        /**
                         * 判断已经预约的人数是否大于要更新的最大可预约人数， reverations > 传进来的number数
                         * 量，则不能更新，要报错
                         * reverations <= 传进来的number数量，则要更新最大可预约数量
                         * */
                        if(oss.getReservations()!=null && oss.getReservations()>os.getNumber()){
                            //3.2不能更新
                            throw new CustomException(MessageConstant.SET_ORDERSETTING_ISNULL);
                        }else{
                            //3.3可更新可预约数量
                            os.setId(oss.getId());
                            ordersettingMapper.updateByPrimaryKeySelective(os);
                        }
                }
            }else{//不存在则添加
                ordersettingMapper.insertSelective(os);
            }
        }
    }


    /**
     *   返回格式 { date: 1, number: 120, reservations: 1 },
     * */
    @Override
    public List<Map> getOrderSettingByDate(Integer year, Integer month) {
        //1.开始日期  2022-2-1
        Date firstDayOfMonth = DateUtils.getFirstDayOfMonth(year, month);
        //2.结束日期  2022-2-28
        Date lastDayOfMonth = DateUtils.getLastDayOfMonth(year, month);
        //3.查询 2022-2-1 --- 2022-2-28
        OrdersettingExample ose=new OrdersettingExample();
        OrdersettingExample.Criteria osec = ose.createCriteria();
        osec.andOrderdateBetween(firstDayOfMonth,lastDayOfMonth);
        List<Ordersetting> ordersettings = ordersettingMapper.selectByExample(ose);
        //4.判空
        if(ordersettings!=null && ordersettings.size()>0){
            //5.创建一个集合装起来
            List<Map> list=new ArrayList<>();
            for(Ordersetting os:ordersettings){
                //6.创建一个map集合装返回格式 { date: 1, number: 120, reservations: 1 }
                Map map=new HashMap();
                map.put("date",os.getOrderdate().getDate());
                map.put("number",os.getNumber()==null?0:os.getNumber());
                map.put("reservations",os.getReservations()==null?0:os.getReservations());
                list.add(map);
            }
            //7.返回出去
            return list;
        }

        return null;
    }


    @Override
    public Ordersetting queryByDate(Date date) {
        OrdersettingExample ose=new OrdersettingExample();
        OrdersettingExample.Criteria osec = ose.createCriteria();
        osec.andOrderdateEqualTo(date);
        List<Ordersetting> list = ordersettingMapper.selectByExample(ose);
        return list!=null && list.size()>0?list.get(0):null;
    }

    @Override
    public List<Ordersetting> selectByDate() {
        return ordersettingMapper.selectByDate();
    }
}
